﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;


namespace PRE.Thongke
{
    public partial class FrmTheophongban : DevExpress.XtraEditors.XtraForm
    {
        #region Variable
        DAL.EntitiesDAL.PhongbanDAL _dal = new DAL.EntitiesDAL.PhongbanDAL();
        BLL.EntitiesBLL.PhongbanBLL _bll = new BLL.EntitiesBLL.PhongbanBLL();

        DataTable _dt = new DataTable();
        DataTable _dt1 = new DataTable();
        #endregion

        #region Inits
        public FrmTheophongban()
        {
            InitializeComponent();
        }
        #endregion

        #region PerformRefresh
        private void PerformRefresh()
        {
            LoadData();
        }
        #endregion

        #region load
        private void LoadData()
        {
            _dt = _dal.Select_Thongke();
            _dt1 = _dal.Select();

            cbo_Phongban.Properties.DataSource = _dt1;

            try
            {
                if (_dt != null)
                {
                    gridControl_Theophongban.DataSource = _dt;

                    AppGridView.SummaryField(gridView_Theophongban, "MANHANVIEN", "Tổng = {0:#,0} mẫu tin", DevExpress.Data.SummaryItemType.Count);                  

                    gridView_Theophongban.OptionsBehavior.ReadOnly = true;
                    gridView_Theophongban.OptionsView.ColumnAutoWidth = true;
                }


            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        #endregion

        #region Form load
       private void FrmTheophongban_Load_1(object sender, EventArgs e)
        {        
            PerformRefresh();
            bto_Loc.Visible = true;
            bto_In.Visible = true;
            bto_Thoat.Visible = true;
        }
        #endregion

        #region Thoát
        private void bto_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Làm tươi
        private void bto_Lamtuoi_Click(object sender, EventArgs e)
        {
            FrmTheophongban_Load_1(null, null);
        }
        #endregion

        #region Lọc
        private void bto_Loc_Click(object sender, EventArgs e)
        {
            try
            {
                string Chonphongban = Convert.ToString(cbo_Phongban.EditValue);
                DataTable _dtSearch = _dal.Select_Thongke_Theophongban(Chonphongban);
                if (_dtSearch != null)
                {


                    gridControl_Theophongban.DataSource = _dtSearch;

                    AppGridView.SummaryField(gridView_Theophongban, "MANHANVIEN", "Tổng = {0:#,0} mẫu tin", DevExpress.Data.SummaryItemType.Count);                  

                    gridView_Theophongban.OptionsView.EnableAppearanceEvenRow = true;
                    gridView_Theophongban.OptionsView.ColumnAutoWidth = true;
                }
                gridControl_Theophongban.DataSource = _dtSearch;

                _dt = _dtSearch;
            }
            catch (Exception ex)
            {

                XtraMessageBox.Show("Lỗi! Không lọc được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region In
        private void bto_In_Click(object sender, EventArgs e)
        {
             string _NgayKy = "TP.Cần Thơ, ngày " + DateTime.Now.Day + " tháng " + DateTime.Now.Month + " năm " + DateTime.Now.Year;
             if (gridView_Theophongban.DataRowCount > 0)
             {
                 DataTable dt = gridView_Theophongban.GetDataRow(0).Table.Clone();

                 for (int i = 0; i < gridView_Theophongban.DataRowCount; i++)
                 {
                     dt.ImportRow(gridView_Theophongban.GetDataRow(i));
                 }
                 Reports.FrmReportView frm = new Reports.FrmReportView();
                 frm._load_XtraThephongban(dt, _NgayKy);
                 frm.Show();
             }
        }
        #endregion
    }
}
